<?php

/**
 * print function
 */

function p($arr){
	echo "<pre>";
	print_r($arr);
	echo "</pre>";
}

function age($YTD){
    if($YTD){
        $YTD = strtotime($YTD);//int strtotime ( string $time [, int $now ] )
        $year = date('Y', $YTD);
        if(($month = (date('m') - date('m', $YTD))) < 0){
            $year++;
        }else if ($month == 0 && date('d') - date('d', $YTD) < 0){
            $year++;
        }
        return date('Y') - $year;
    }else{
        return null;
    }

}

function guid(){
    if (function_exists('com_create_guid')){
        $uid = com_create_guid();
        $uid = str_replace(array('{','}','-'), array('','',''), $uid);
        return strtolower($uid);

    }else{
        mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
        $charid = strtoupper(md5(uniqid(rand(), true)));
        $hyphen = chr(45);// "-"
        $uuid = chr(123)// "{"
                .substr($charid, 0, 8).$hyphen
                .substr($charid, 8, 4).$hyphen
                .substr($charid,12, 4).$hyphen
                .substr($charid,16, 4).$hyphen
                .substr($charid,20,12)
                .chr(125);// "}"
        return strtolower(str_replace(array('{','}','-'), array('','',''), $uuid));
    }
}

function file_upload($little_img,$pk_doctor_id,$time_out = 5){
    if(strstr($_SERVER['SCRIPT_NAME'], 'test')){
      $uploade_url = 'http://dev.picserver.o2omedical.com:8080/o2o_pic_server/file-upload.do';
    }else{
      $uploade_url = 'http://picserver.o2omedical.com:8080/o2o_pic_server/file-upload.do';
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, $time_out);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_URL, $uploade_url);
    //most importent curl assues @filed as file field
    $post_array = array(
        "my_file"=>"@".$little_img,
        "upload"=>"Upload",
        "pic_type"=>0,
        "user_id"=>$pk_doctor_id,
        "user_type"=>0,
        "pic_purpose"=>1,
        "token"=>"b226d434-052a-4a14-9c59-917173a12864"
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_array);
    $response = curl_exec($ch);
    return json_decode($response,true);
}

function log_num(){
    array(
        '001'=>'login_seccess','002'=>'login_faile','003'=>'login_out',

        '004'=>'save_prescription','005'=>'find_prescription','006'=>'del_prescription',
        '007'=>'list_prescription',

        '008'=>'save_patient','009'=>'connect_patient','010'=>'list_patient',

        '011'=>'search_westermedicine','012'=>'search_mongowestermedicine','013'=>' ','014'=>'add_westermedicine','015'=>'update_westermedicine',

        '016'=>'save_article','017'=>'list_article','018'=>'find_article','019'=>'update_article','020'=>'del_article','upload_pic');
}

/*
*  PHP缩略图生成
*  等比压缩
*  支持格式Gif/Jpeg/Png
*/
function makeThumbnail($srcImgPath,$targetImgPath,$targetW=10,$targetH=10,$bDengbi=true){
     $imgSize = GetImageSize($srcImgPath);
     $imgType = $imgSize[2];
     //使函数不向页面输出错误信息
     switch ($imgType)
    {
      case 1:
        $srcImg = @ImageCreateFromGIF($srcImgPath);
        break;
      case 2:
        $srcImg = @ImageCreateFromJpeg($srcImgPath);
        break;
      case 3:
        $srcImg = @ImageCreateFromPNG($srcImgPath);
        break;
    }
     //取源图象的宽高
    $srcW = ImageSX($srcImg);
    $srcH = ImageSY($srcImg);

    if($srcW > $srcH){
      $targetW = $srcW;
    }else{
      $targetH = $srcH;
    }

    if($srcW>$targetW || $srcH>$targetH)
    {
      if($bDengbi){//等比压缩
        if($targetW>10){//定宽
          $targetH = $srcH*$targetW/$srcW;
        }else if($targetH>10){//定高
          $targetW = $srcW*$targetH/$srcH;
        }else{
        return false;
        }
        if($targetH<11 ||$targetW<11) return false;
      }//end
      $targetX = 0;
      $targetY = 0;
      if ($srcW > $srcH)
      {
        $finaW=$targetW;
        $finalH=round($srcH*$finaW/$srcW);
        $targetY=floor(($targetH-$finalH)/2);
      }
      else
      {
        $finalH=$targetH;
        $finaW=round($srcW*$finalH/$srcH);
        $targetX=floor(($targetW-$finaW)/2);
      }
       //function_exists 检查函数是否已定义
       //ImageCreateTrueColor 本函数需要GD2.0.1或更高版本
      if(function_exists("ImageCreateTrueColor"))
      {
        $targetImg=ImageCreateTrueColor($targetW,$targetH);
      }
      else
       {
        $targetImg=ImageCreate($targetW,$targetH);
      }
      $targetX=($targetX<0)?0:$targetX;
      $targetY=($targetX<0)?0:$targetY;
      $targetX=($targetX>($targetW/2))?floor($targetW/2):$targetX;
      $targetY=($targetY>($targetH/2))?floor($targetH/2):$targetY;
      //背景颜色默认白色
      $white = ImageColorAllocate($targetImg, 255,255,255);
      ImageFilledRectangle($targetImg,0,0,$targetW,$targetH,$white);
      /*
          PHP的GD扩展提供了两个函数来缩放图象：
          ImageCopyResized 在所有GD版本中有效，其缩放图象的算法比较粗糙，可能会导致图象边缘的锯齿。
          ImageCopyResampled 需要GD2.0.1或更高版本，其像素插值算法得到的图象边缘比较平滑，
                               该函数的速度比ImageCopyResized慢。
      */
      if(function_exists("ImageCopyResampled"))
      {
        ImageCopyResampled($targetImg,$srcImg,$targetX,$targetY,0,0,$finaW,$finalH,$srcW,$srcH);
      }
      else
      {
        ImageCopyResized($targetImg,$srcImg,$targetX,$targetY,0,0,$finaW,$finalH,$srcW,$srcH);
      }
       switch ($imgType) {
        case 1:
          ImageGIF($targetImg,$targetImgPath,100);
          break;
        case 2:
          ImageJpeg($targetImg,$targetImgPath,100);
          break;
        case 3:
          ImagePNG($targetImg,$targetImgPath,100);
          break;
      }
      ImageDestroy($srcImg);
      ImageDestroy($targetImg);
    }
     else //不超出指定宽高则直接复制
    {
      copy($srcImgPath,$targetImgPath);
      ImageDestroy($srcImg);
    }
}

function send_sms_yun($apikey, $tpl_id, $tpl_value, $mobile){
    $url="http://yunpian.com/v1/sms/tpl_send.json";
    $encoded_tpl_value = urlencode("$tpl_value");  //tpl_value需整体转义
    $mobile = urlencode("$mobile");
    $post_string="apikey=$apikey&tpl_id=$tpl_id&tpl_value=$encoded_tpl_value&mobile=$mobile";

    $data = "";
    $info=parse_url($url);
    $fp=fsockopen($info["host"],80,$errno,$errstr,30);
    if(!$fp){
        return $data;
    }
    $head="POST ".$info['path']." HTTP/1.0\r\n";
    $head.="Host: ".$info['host']."\r\n";
    $head.="Referer: http://".$info['host'].$info['path']."\r\n";
    $head.="Content-type: application/x-www-form-urlencoded\r\n";
    $head.="Content-Length: ".strlen(trim($post_string))."\r\n";
    $head.="\r\n";
    $head.=trim($post_string);
    $write=fputs($fp,$head);
    $header = "";
    while ($str = trim(fgets($fp,4096))) {
        $header.=$str;
    }
    while (!feof($fp)) {
        $data .= fgets($fp,4096);
    }
    return $data;
}

function postSMS($mobiel,$data){
     $post_data = array();
     $post_data['account'] = iconv('UTF-8', 'GB2312',"outuou");
     $post_data['pswd'] = iconv('UTF-8', 'GB2312',"O2o592864999");
     $post_data['mobile'] = $mobiel;
     $post_data['msg'] = $data;
     $url='http://222.73.117.158/msg/HttpBatchSendSM?';
     $o="";
     foreach ($post_data as $k=>$v)
     {
        $o.= "$k=".urlencode($v)."&";
     }
     $post_data=substr($o,0,-1);
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt($ch, CURLOPT_HEADER, 0);
     curl_setopt($ch, CURLOPT_URL,$url);
     curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
     $result_ = curl_exec($ch);
     curl_close($ch);
}

?>
